﻿<?php
include_once $_SERVER ['DOCUMENT_ROOT'] . '/paying/sdk/acp_service.php';

?>
<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>银联在线交易测试-结果</title>
<style type="text/css">
body table tr td {
	font-size: 14px;
	word-wrap: break-word;
	word-break: break-all;
	empty-cells: show;
}
</style>
</head>
<body>
	<table width="800px" border="1" align="center">
		<tr>
			<th colspan="2" align="center">银联在线交易测试-后台通知交易结果</th>
		</tr>
			<?php
			foreach ( $_POST as $key => $val ) {
				?>
			<tr>
			<td width='30%'><?php echo isset($mpi_arr[$key]) ?$mpi_arr[$key] : $key ;?></td>
			<td><?php echo $val ;?></td>
		</tr>
			<?php }?>
			<tr>
			<td width='30%'>验证签名</td>
			<td><?php	
			Log::info("支付Starting!");
			if (isset ( $_POST ['signature'] )) {				
				//echo com\unionpay\acp\sdk\AcpService::validate ( $_POST ) ? '验签成功' : '验签失败';
				$order_no = $_POST ['orderId']; //其他字段也可用类似方式获取
				$respCode = $_POST ['respCode']; //判断respCode=00或A6即可认为交易成功
				if($respCode=='00'||$respCode=='A6'){
					$attach_money = round($_POST['txnAmt'] / 100, 2);
					//Log::info(__METHOD__ . __LINE__ . json_encode());
					$question_id = $_POST['reqReserved'];//"question_id"
					$question = Post::find($question_id);
					$order = $question->getOrder($question_id);
					try{
						$question->status = Post::STATUS_NEW;
						$question->save();
						if ($order) {
							Log::info("order info=".json_encode($_POST));
							$order->pingpp_id 		= '';
							$order->state 			= Order::STATE_PAID;
							$order->pay_status 		= Order::PAY_STATUS_PAIS;
							$order->pay_at 			= date('Y-m-d H:i:s');
							$order->channel 		= '银联';
							$order->pay_price		= $attach_money;
							$order->save();		
						}									
						$lawyer = $question->getAssignedLawyer();
						if ($lawyer) {
							$message = sprintf("%s律师的%s", $lawyer->realname, $question->getType());
						} else {
							$message = $question->getType();
						}
						$transaction=WalletTransaction::getWalletTransaction($question->creator_id,$this->getOrderType($question->type),$order_no,WalletTransaction::DIRECTION_OUT);
					} catch (Exception $ex) {
						Log::error($ex);
						return $this->makeView('errors.400', $ex, 400);
					}
					$event = 'pay_post';
					$argument = $question;
					if ($event) {
						Event::fire($event, $argument);
					}
				}else{
					return $this->makeView('errors.400', '支付失败', 400);
				}
			} else {
				Log::warn('签名为空');
			}
			Log::info("支付OK!");
			?></td>
		</tr>
	</table>
	<?php 
		//如果卡号我们业务配了会返回且配了需要加密的话，请按此方法解密
// 		if(array_key_exists ("accNo", $_POST)){
// 			$accNo = com\unionpay\acp\sdk\AcpService::decryptData($_POST["accNo"]);
// 			echo  "accNo=" . $accNo . "<br>\n";
// 		}
	?>
</body>
</html>